# Adogs
# 时间: 2021/7/25 14:41
import pygame
from actor import WalkDir
from actor.RedArmy_walk_soldier import RedArmyWalkSoldier
from actor.japanese_walk_soldier import JapaneseWalkSoldier
from actor.treasure_sprite import TreasureSprite
from actor.xiao_tie import XiaoTie
from pytmx import load_pygame
from scene import BaseScene, ScenePassStatus
from scene.task import TaskStatus
from scene.task.task1_take_prizes import TakePrizesTask
from scene.task.task2_mine_set import KillJapaneseTask
from scene.task.task3_transmit_message import TransmitMessageTask
from scene.task.task4_shoot import ShootTask
from scene.task.task5_way_to_win import WayToWinTask


class TunnelWarScene(BaseScene):
    """
    第一个场景类
    方法:
    1.__init__(xiao_tie)
    2.init_actor()
    3.init_task()
    4.draw_actor()
    5.run(down_flag, key_list)
    6.get_pass_status()
    """
    def __init__(self, xiao_tie):
        """
        初始化函数
        :param xiao_tie: 主角人物
        """
        super(TunnelWarScene, self).__init__(xiao_tie, "resources/tmx/map_1.png")
        self.tiled_map = load_pygame("resources/tmx/map_1_1.tmx")       # 加载地图

        # 加载背景音乐
        pygame.mixer.music.load("resources/sound/十送红军.mp3")
        pygame.mixer.music.play()

        # 加载人物对象组
        self.obstacle_group = pygame.sprite.Group()
        self.japanese_group = pygame.sprite.Group()
        self.treasure_group = pygame.sprite.Group()
        self.red_army_group = pygame.sprite.Group()

        self.init_actor()   # 初始化人物
        self.init_task()    # 初始化任务

    def init_actor(self):
        """
        初始化人物
        :return: 无返回值
        """
        for group in self.tiled_map.objectgroups:

            if group.name == '日本人':
                for obj in group:
                    japanese = JapaneseWalkSoldier(obj.x, obj.y)
                    self.japanese_group.add(japanese)

            elif group.name == '宝箱':
                for obj in group:
                    treasure = TreasureSprite(obj.x, obj.y)
                    treasure.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                    self.treasure_group.add(treasure)

            elif group.name == '障碍物':
                for obj in group:
                    obstacle = pygame.sprite.Sprite()
                    obstacle.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                    self.obstacle_group.add(obstacle)

            elif group.name == '解放军':
                for obj in group:
                    red_army = RedArmyWalkSoldier(obj.x, obj.y)
                    if obj.name == '铁头' or obj.name == '国强':
                        red_army.name = obj.name
                        if red_army.name == '铁头':
                            self.tie_tou = red_army
                            red_army.walk_action.set_dir(WalkDir.left)
                        else:
                            self.guo_qiang = red_army
                    else:
                        self.red_army_group.add(red_army)

            elif group.name == '小铁':
                for obj in group:
                    self.xiao_tie.set_pos(obj.x, obj.y)

    def init_task(self):
        """
        初始化任务
        :return: 无返回值
        """
        self.tasks = []
        self.font = pygame.font.Font("resources/font/975宋体.ttf", 13)    # 加载字体

        # 添加任务
        self.tasks.append(TakePrizesTask(self.xiao_tie, self.treasure_group, self.font))
        self.tasks.append(KillJapaneseTask(self.xiao_tie, self.japanese_group, self.font))
        self.tasks.append(TransmitMessageTask(self.xiao_tie, self.guo_qiang, self.tie_tou, self.font))
        self.tasks.append(ShootTask(self.xiao_tie, self.japanese_group, self.font))

        self.task_id = 0

    def draw_actor(self):
        """
        绘制人物
        :return: 无返回值
        """
        for japanese in self.japanese_group:
            japanese.draw(self.current_surface, self.view_x, self.view_y)

        for treasure in self.treasure_group:
            treasure.draw(self.current_surface, self.view_x, self.view_y)

        for red_army in self.red_army_group:
            red_army.draw(self.current_surface, self.view_x, self.view_y)

        self.xiao_tie.draw(self.current_surface, self.view_x, self.view_y)

        self.tasks[self.task_id].draw(self.current_surface, self.view_x, self.view_y)   # 绘制任务

    def run(self, down_flag, key_list):
        """
        运行函数
        :param down_flag: 判断是否点击按键
        :param key_list: 判断是否有键按下
        :return: 无返回值
        """
        for japanese in self.japanese_group:
            japanese.run(down_flag, key_list, self.obstacle_group)

        for red_army in self.red_army_group:
            red_army.run(down_flag, key_list, self.obstacle_group)

        self.tasks[self.task_id].do_task(down_flag, key_list)

        self.xiao_tie.run(down_flag, key_list, self.obstacle_group, self.japanese_group, self.red_army_group)

        task_status = self.tasks[self.task_id].get_status()
        if task_status == TaskStatus.win_over:
            self.task_id += 1
            if self.task_id >= len(self.tasks):
                self.pass_status = ScenePassStatus.next_sence
                pygame.mixer.music.stop()

        elif task_status == TaskStatus.fail_over:
            self.pass_status = ScenePassStatus.final_fail
            pygame.mixer.music.stop()

    def get_pass_status(self):
        """
        获取场景状态
        :return: 场景状态
        """
        if self.pass_status == ScenePassStatus.final_win:
            return self.pass_status, "WinScene"
        elif self.pass_status == ScenePassStatus.final_fail:
            return self.pass_status, "FailScene"
        elif self.pass_status == ScenePassStatus.next_sence:
            self.pass_status = ScenePassStatus.level_pass
            return self.pass_status, "TunnelWarScene2"
        else:
            return self.pass_status, None
